package com.erp.module.zhuanMaiDianTu

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.erp.R
import com.erp.bean.ZhuanMaiDianBean
import com.erp.myAdapter.ZhuanMaiDianAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_zhuanmaidian.*
import java.util.*

/**
 * Created by Administrator on 2018/1/26/026.
 * 作者：刘洋
 * 专卖店列表
 */

class FragmentZhuanMaiDianTu : BaseKFragment(), PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, AdapterView.OnItemClickListener, View.OnClickListener,ZhuanMaiDianTuContract.ZhuanMaiDianView {
    lateinit var mlist: MutableList<ZhuanMaiDianBean>
    lateinit var adapter: ZhuanMaiDianAdapter
    var updatedialog: AlertDialog? = null
    private var sousuocode = 0
    private var sousuo = ""
    private val f = "FragmentZhuanMaiDianTu"
    lateinit var P: IZhuanMaiDianP
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_zhuanmaidian
    }
    override fun initData() {
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        LvZhuanMaiDian.onItemClickListener = this
        initsp()
        EtZhiNan.setOnEditorActionListener { _, actionId, _ ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
                    sousuo()
                }
                else -> isOK = false
            }
            isOK
        }
        IvSouSuo.setOnClickListener(this)
        mlist = ArrayList()
        adapter = ZhuanMaiDianAdapter(mlist, context!!)
        P= IZhuanMaiDianP(this,context!!,mlist)
        LvZhuanMaiDian.adapter = adapter
        P.gainData("ZhuanMaiDian")
        Pull.headerRefreshing()
    }

    fun  qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    private fun initsp() {
        val listsp = ArrayList<String>()
        listsp.add("用户名")
        listsp.add("登录别名")
        listsp.add("专卖店ID")
        listsp.add("区域")
        listsp.add("省份")
        val adapter = ArrayAdapter(activity!!, R.layout.lv_sp_tvitem, listsp)
        SpZhuanmaidian.adapter = adapter
    }


    fun sousuo() {
        sousuo = EtZhiNan.text.toString()
        when {
            SpZhuanmaidian.selectedItem.toString() == "用户名" -> sousuocode = 0
            SpZhuanmaidian.selectedItem.toString() == "登录别名" -> sousuocode = 1
            SpZhuanmaidian.selectedItem.toString() == "专卖店ID" -> sousuocode = 2
            SpZhuanmaidian.selectedItem.toString() == "区域" -> sousuocode = 3
            SpZhuanmaidian.selectedItem.toString() == "省份" -> sousuocode = 4
        }
        P.chaxun(sousuocode,sousuo)
    }


    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        val intent = Intent(activity, ZhuanmaidiantuActivity::class.java)
        intent.putExtra("oid", mlist[position].uid)
        intent.putExtra("code", 4)
        startActivity(intent)
    }

    override fun onClick(v : View) {
        when(v){
            IvSouSuo -> {
                updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
                sousuo()
            }
        }
    }
    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
}
